package com.ruike.alisurface.ui.answerQuestion.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ruike.alisurface.R;
import com.ruike.alisurface.ui.answerQuestion.bean.QuestionProductBean;
import com.ruike.alisurface.utils.GlideImageLoader;

import java.util.List;
import java.util.Locale;

/**
 * Author: voodoo
 * CreateDate: 2023/11/22 022上午 11:46
 * Description: 答题商品列表
 */
public class QuestionProductAdapter extends RecyclerView.Adapter<QuestionProductAdapter.ProductViewHolder> {

    Context context;
    List<QuestionProductBean.DataBean> list;
    OnProductClickListener onProductClickListener;

    public QuestionProductAdapter(Context context, List<QuestionProductBean.DataBean> list, OnProductClickListener onProductClickListener) {
        this.context = context;
        this.list = list;
        this.onProductClickListener = onProductClickListener;
    }

    public void setItem(List<QuestionProductBean.DataBean> productList) {
        this.list = productList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_question_product, null, false);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductViewHolder productViewHolder, int i) {
        QuestionProductBean.DataBean dataBean = list.get(i);
        // 设置图片
        GlideImageLoader.loadImage(context, dataBean.getProduct_pic(), productViewHolder.goodsImageImgv);
        // 设置是否售罄
        if (dataBean.getSlot_count() <= 0) {
            productViewHolder.sellOutImgv.setVisibility(View.VISIBLE);
        }
        // 设置商品名称
        productViewHolder.goodsNameTv.setText(dataBean.getProduct_name());
        productViewHolder.ruleTv.setText(String.format(Locale.ENGLISH, "需答%d题，正确%d题", dataBean.getSet_question_count(), dataBean.getSet_pass_count()));

        productViewHolder.itemView.setOnClickListener(view -> {
            if (onProductClickListener != null) {
                onProductClickListener.onProductItemClickListener(i, dataBean);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ProductViewHolder extends RecyclerView.ViewHolder {

        ImageView goodsImageImgv;
        ImageView sellOutImgv;
        TextView goodsNameTv;
        TextView ruleTv;

        public ProductViewHolder(@NonNull View itemView) {
            super(itemView);

            goodsImageImgv = itemView.findViewById(R.id.itemQuestionProduct_goodsImage_imgv);
            sellOutImgv = itemView.findViewById(R.id.itemQuestionProduct_clear_imgv);
            goodsNameTv = itemView.findViewById(R.id.itemQuestionProduct_goodsName_tv);
            ruleTv = itemView.findViewById(R.id.itemQuestionProduct_rule_tv);

        }
    }

    public interface OnProductClickListener {
        /**
         * 点击的item
         *
         * @param index    点击的索引
         * @param dataBean 点击的商品Bean
         */
        void onProductItemClickListener(int index, QuestionProductBean.DataBean dataBean);
    }

}
